package football.haha.com.footballtv.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import football.haha.com.footballtv.FootballApplication;
import football.haha.com.footballtv.R;
import football.haha.com.footballtv.bean.BeanHighLights;
import football.haha.com.footballtv.bean.BeanLookBack;
import football.haha.com.footballtv.bean.BeanSchedule;
import football.haha.com.footballtv.bean.BeanSecondFootball;
import football.haha.com.footballtv.utils.ApiUtils;
import football.haha.com.footballtv.utils.ImageToos;
import football.haha.com.footballtv.utils.NetUtils;
import football.haha.com.footballtv.utils.Tools;
import football.haha.com.footballtv.view.SmoothVorizontalScrollView;

/**
 * Created by haha on 2017/10/7.
 */

public class ScheduleMoreActivity extends BaseActivity {

    @Bind(R.id.fl_schedule_day)
    FrameLayout flScheduleDay;
    @Bind(R.id.svsv_day)
    SmoothVorizontalScrollView svsvDay;
    @Bind(R.id.fl_schedule_content)
    FrameLayout flScheduleContent;
    @Bind(R.id.svsv_schedule)
    SmoothVorizontalScrollView svsvSchedule;
    @Bind(R.id.fl_main)
    FrameLayout flMain;

    public List<BeanSchedule.ContentsBean> scheduleContentList = new ArrayList<>();
    public ArrayList<View> scheduleContentItemList = new ArrayList<>();
    private ArrayList<CheckBox> scheduleDayItemList = new ArrayList<>();
    private String folderCode = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule_fight_more);
        ButterKnife.bind(this);

        createOnlyBorderView(flMain);
        scale = 1.0f;

        folderCode = getIntent().getStringExtra("folderCode");

        addScheduleDay();
    }

    private void getScheduleDayData(String day) {
        Map<String, String> params = new HashMap<>();
        params.put("command", "program");
        params.put("date", day);
        params.put("folderCode", folderCode);
        params.put("busCode", "IPS");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGScheduleDayData, params, BeanSchedule.class, "加载中...", this);
    }


    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            if (scheduleDayItemList.contains(view)) {
                for (int i = 0; i < scheduleDayItemList.size(); i++) {
                    scheduleDayItemList.get(i).setChecked(false);
                }
                ((CheckBox) view).setChecked(true);
                borderView.setBorderBitmapResId(R.drawable.fous_left_date_tab, (int) getResources().getDimension(R.dimen.px60));
                getScheduleDayData(Tools.getPastDateYYMMDD(scheduleDayItemList.indexOf(view)-3));
            }else{
                borderView.setBorderBitmapResId(R.drawable.bg_shape_white);
            }
        }
        super.onFocusChange(view, b);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        BeanSchedule.ContentsBean contentsBean = scheduleContentList.get(scheduleContentItemList.indexOf(view));
        getAuthPlayUrl(contentsBean.getName(), folderCode, contentsBean.getContentId(),
                contentsBean.getContentType(), contentsBean.getEndTime());
    }

    public void addScheduleDay() {
        int allHeight = 0;
        for (int i = 3; i > -5; i--) {
            CheckBox itemDay = (CheckBox) View.inflate(this, R.layout.item_day, null);
            itemDay.setOnFocusChangeListener(this);
            itemDay.setId(i + 2000);
            itemDay.setText(Tools.getPastDate(i));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int) getResources().getDimension(R.dimen.px180),
                    (int) getResources().getDimension(R.dimen.px100));

            int topMargin = (i+4) * (int) getResources().getDimension(R.dimen.px90);
            int leftMargin = (int) getResources().getDimension(R.dimen.px80);

            params.topMargin = topMargin;
            params.leftMargin = leftMargin;

            flScheduleDay.addView(itemDay, params);
            allHeight = topMargin + params.height + (int) getResources().getDimension(R.dimen.px70);

            scheduleDayItemList.add(itemDay);
        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, allHeight);
        flScheduleDay.setLayoutParams(params);
        showViewByHandler(scheduleDayItemList.get(3));
    }

    public void addScheduleContentList(BeanSchedule beanSchedule) {
        scheduleContentItemList.clear();
        flScheduleContent.removeAllViews();
        scheduleContentList = beanSchedule.getContents();
        int allHeight = 0;
        for (int i = 0; i < scheduleContentList.size(); i++) {
            View itemSchedule = View.inflate(this, R.layout.item_schedule_football, null);
            itemSchedule.setOnFocusChangeListener(this);
            itemSchedule.setOnClickListener(this);
            itemSchedule.setId(i + 2000);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    (int) getResources().getDimension(R.dimen.px140));

            int topMargin = i * (int) getResources().getDimension(R.dimen.px150);
            int leftMargin = (int) getResources().getDimension(R.dimen.px320);

            params.topMargin = topMargin;
            params.leftMargin = leftMargin;
            params.rightMargin = (int) getResources().getDimension(R.dimen.px120);

            scheduleContentItemList.add(itemSchedule);
            flScheduleContent.addView(itemSchedule, params);
            allHeight = topMargin + params.height + (int) getResources().getDimension(R.dimen.px150);

            BeanSchedule.ContentsBean contentsBean = beanSchedule.getContents().get(i);
            TextView tvName = (TextView) itemSchedule.findViewById(R.id.tv_name);
            tvName.setText(contentsBean.getName());
            TextView tvState = (TextView) itemSchedule.findViewById(R.id.tv_state);
            if (TextUtils.equals("1", contentsBean.getProgramType())){
                tvState.setText("正在直播");
                tvState.setTextColor(0xff48C629);
            }else if (TextUtils.equals("2", contentsBean.getProgramType())){
                tvState.setText("已结束");
                tvState.setTextColor(0xff268FFB);
            }else {
                tvState.setText(Tools.getSchedueStartTime(contentsBean.getStartTime()));
                tvState.setTextColor(0xff8D93A2);
            }

            TextView tvResult = (TextView) itemSchedule.findViewById(R.id.tv_result);
            tvResult.setText(contentsBean.getScore());

            TextView tvLeftTeamName = (TextView) itemSchedule.findViewById(R.id.tv_left_team_name);
            tvLeftTeamName.setText(contentsBean.getHomeTeamName());

            TextView tvRightTeamName = (TextView) itemSchedule.findViewById(R.id.tv_right_team_name);
            tvRightTeamName.setText(contentsBean.getAwayTeamName());

            ImageView ivLeftIcon = (ImageView) itemSchedule.findViewById(R.id.iv_left_team_icon);
            ImageView ivRightIcon = (ImageView) itemSchedule.findViewById(R.id.iv_right_team_icon);

            ImageToos.setImageWithUrl(contentsBean.getHomeTeamLogo(), ivLeftIcon, FootballApplication.getOptions(R.drawable.default_team));
            ImageToos.setImageWithUrl(contentsBean.getAwayTeamLogo(), ivRightIcon, FootballApplication.getOptions(R.drawable.default_team));
        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, allHeight);
        flScheduleContent.setLayoutParams(params);
    }

    @Override
    public void netBack(int requestTag, Object object) {
        super.netBack(requestTag, object);
        if (requestTag == ApiUtils.TAGScheduleDayData) {
            BeanSchedule beanSchedule = (BeanSchedule) object;
            if (beanSchedule == null) {
                return;
            }
            addScheduleContentList(beanSchedule);
        }
    }
}
